package com.wontube.smr.remote;

import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.GenreSelectBO;
import com.wontube.smr.bo.sites.MVSelectBO;
import com.wontube.smr.client.IMVHandler;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.MVChart;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.bo.MovieSelectDao;


/**
 * 音乐接口实现
 * 
 * @author hewc@wondershare.cn
 * 
 */
@NoLoginSupport
public final class MVHandler extends BasicHandler implements IMVHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> selectAllGenres() {
		return GenreSelectBO.selectRemendedMusicGenres();
	}
	
	@Override
	@CacheSupport
	public MovieItems searchMusic(String keyword, Double dbFactor, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}
		
		try {
			return MVSelectBO.searchMusics(keyword, dbFactor.doubleValue(), pageInfo);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Movie getMV(String movieId) {
		if (StringUtils.isNullOrEmpty(movieId)) {
			return null;
		}
		return MovieSelectDao.getMovie(movieId);
	}

	@Override
	@CacheSupport
	public MovieItems listRelated(String mvId, PagenateInfo pageInfo) {
		if (StringUtils.isNullOrEmpty(mvId)) {
			return null;
		}
		return MVSelectBO.listRelated(mvId, pageInfo);
	}
	
	/**
	 * 全流派音乐 
	 * 由于暂时拿不到 youtube 的数据，只从数据库取
	 *  
	 */
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 3600)
	public MovieItems selectMusic(String updatePeriod, String genreName, Double dbFactor, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}
		
		//修正Others 等重复genre
		genreName = GenreSelectBO.joinMusicGenres(genreName);
		return MVSelectBO.selectMusics(updatePeriod, genreName, dbFactor, pageInfo);
	}

	@Override
	public MovieItems selectWeeklyMusics(String genreName, Double dbFactor, PagenateInfo pageInfo) {
		return selectMusic(MVChart.UPDATE_PERIOD_WEEKLY, genreName, dbFactor, pageInfo);
	}

	@Override
	public MovieItems selectRecentMusics(String genreName, Double dbFactor, PagenateInfo pageInfo) {
		return selectMusic(MVChart.UPDATE_PERIOD_RECENT, genreName, dbFactor, pageInfo);
	}	
}
